package com.nanrui.mutualinductor.data;


import java.io.Serializable;
import java.util.Arrays;

public abstract class DKPacket
        implements Serializable {
    private static final long serialVersionUID = -1059722831485897514L;
    private int count = 1;
    private Short frameNo;
    private short frameType;
    private short packetType;

    public DKPacket(short paramShort1, short paramShort2) {
        this.frameType = ((short) paramShort1);
        this.packetType = ((short) paramShort2);
    }

    public static DKPacket instance(byte[] paramArrayOfByte) {
        Object localObject = null;
        switch (paramArrayOfByte[7] & 0xFF) {

            case 87:
                if ((paramArrayOfByte[5] & 0xFF)==01){
                    // PT传统法误差测试
                    localObject = new Packet_57_01();
                }else if ((paramArrayOfByte[5] & 0xFF)==03){
                    localObject = new Packet_57_03();
                }else {
                    //电磁式PT低校高法误差测试
                    localObject = new Packet_57();
                }
                break;
            case 86:
                if ((paramArrayOfByte[5] & 0xFF)==01){
                    // CT传统法误差测试
                    localObject = new Packet_56_01();
                }else if ((paramArrayOfByte[5] & 0xFF)==03){
                    localObject = new Packet_56_03();
                }else {
                    //CT低校高法误差测试
                    localObject = new Packet_56();
                }
                break;

            case 20:
                localObject = new Packet_14();
                break;
            case 19:
                localObject = new Packet_13();
                break;
            case 17:
                localObject = new Packet_11();
                break;

            case 0:
                break;
            default:

        }

        if (localObject == null) {
            return null;
        }
        ((DKPacket) localObject).unpackContent(Arrays.copyOfRange(paramArrayOfByte, 8, paramArrayOfByte.length - 3));
        return (DKPacket) localObject;
    }

    public int getCount() {
        return this.count;
    }

    public Short getFrameNo() {
        return this.frameNo;
    }


    public byte[] pack(short paramShort) {
        byte[] arrayOfByte1 = packContent();
        byte[] arrayOfByte2 = new byte[arrayOfByte1.length + 11];
        arrayOfByte2[0] = ((byte) -91);
        arrayOfByte2[1] = ((byte) 90);
        arrayOfByte2[2] = ((byte) (byte) arrayOfByte1.length);
        arrayOfByte2[3] = ((byte) (byte) (arrayOfByte1.length >> 8));
        int i = arrayOfByte2[2];
        short s = arrayOfByte2[3];
        arrayOfByte2[4] = ((byte) (byte) paramShort);
        int j = arrayOfByte2[4];
        arrayOfByte2[5] = this.frameNo.byteValue();//试验类型（无符号整数）（按类型顺序依次：传统法：01、电磁式PT低校高法：02、电容式PT低校高法：03）
        int k = arrayOfByte2[5];
        arrayOfByte2[6] = ((byte) (byte) this.frameType);//都传了三
        paramShort = arrayOfByte2[6];
        arrayOfByte2[7] = ((byte) (byte) this.packetType);// ct  86   pt  87  0x57  0x56
        i = 0 + (i & 0xFF) + (s & 0xFF) + (j & 0xFF) + (k & 0xFF) + (paramShort & 0xFF) + (arrayOfByte2[7] & 0xFF);
        paramShort = 0;
        s = (short) arrayOfByte1.length;
        while (paramShort < s) {
            arrayOfByte2[(paramShort + 7 + 1)] = ((byte) arrayOfByte1[paramShort]);
            i += (arrayOfByte1[paramShort] & 0xFF);
            paramShort++;
        }
        arrayOfByte2[(arrayOfByte1.length + 7 + 1)] = ((byte) (byte) i);
        arrayOfByte2[(arrayOfByte1.length + 7 + 2)] = ((byte) (byte) (i >> 8));
        arrayOfByte2[(arrayOfByte1.length + 7 + 3)] = ((byte) -106);
        return arrayOfByte2;
    }

    abstract byte[] packContent();

    public void setCount(int paramInt) {
        this.count = paramInt;
    }

    public void setFrameNo(short paramShort) {
        this.frameNo = Short.valueOf(paramShort);
    }

    abstract void unpackContent(byte[] paramArrayOfByte);
}
